package com.elong.businesstravel.modules.member;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshListView;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase.Mode;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.baseactivity.BaseActivity;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.entitys.MessageNotice;
import com.elong.businesstravel.modules.init.InitActivity;
import com.elong.businesstravel.modules.member.adapter.MessageCenterAdapter;
import com.elong.businesstravel.protocol.login.MemberGetNoticeRequest;
import com.elong.businesstravel.protocol.login.MemberGetNoticeResponse;

/**
 * 消息中心
 * 
 * @author lenovo
 * 
 */
public class MessageCenterActivity extends BaseActivity {

	private PullToRefreshListView mPullToRefreshListView;
	private MessageCenterAdapter mMessageCenterAdapter;

	private List<MessageNotice> mMessageNotices = new ArrayList<MessageNotice>();
	private int mPage = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message_center);

		init();
	}

	@Override
	protected void init() {

		mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.listView);
		mPullToRefreshListView.setMode(Mode.PULL_FROM_END);
		mPullToRefreshListView.getRefreshableView().setDividerHeight(0);
		mPullToRefreshListView.getRefreshableView().setSelector(android.R.color.transparent);
		mPullToRefreshListView.getRefreshableView().setDivider(getResources().getDrawable(android.R.color.transparent));
		mMessageCenterAdapter = new MessageCenterAdapter(mContext,
				mMessageNotices);
		mPullToRefreshListView.setAdapter(mMessageCenterAdapter);

		setListener();
		setTitleNavBar();

		memberGetNotice(false);
	}

	@Override
	protected void setListener() {
		mPullToRefreshListView
				.setOnRefreshListener(new OnRefreshListener<ListView>() {

					@Override
					public void onRefresh(
							PullToRefreshBase<ListView> refreshView) {

						memberGetNotice(true);

						HandlerPostUI.getHandlerPostUI(new Runnable() {

							@Override
							public void run() {
								mPullToRefreshListView.onRefreshComplete();
							}
						});
					}
				});
	}

	@Override
	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage("消息中心");
		titleNavBarView.setLeftDrawableImage(R.drawable.btn_title_back);
		titleNavBarView.setLeftButtonImageString(0, "返回", R.color.text_blue);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				quit();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	private void memberGetNotice(final boolean moreFlag) {

		if (moreFlag) {
			mPage++;
		} else {
			mPage = 1;
		}

		NetworkClient.getNetworkClient().PostRequest(
				new MemberGetNoticeRequest(mContext, mPage + ""),
				new MemberGetNoticeResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						HandlerPostUI.getHandlerPostUI(new Runnable() {

							@Override
							public void run() {
								if (moreFlag) {
									// 更多
									if(0 == messageNotices.size()){
									HandlerToastUI.getHandlerToastUI(mContext, "没有更多信息");	
									}else{
									mMessageNotices.addAll(messageNotices);
									}
								} else {
									// 刷新
									mMessageNotices.clear();
									mMessageNotices.addAll(messageNotices);
								}
								mMessageCenterAdapter.notifyDataSetChanged();
							}
						});

					}
				});
	}

	private void quit() {
		if (mApplication.mFlagHome) {
			finish();
		} else {
			Intent intent = new Intent(mContext, InitActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			finish();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			quit();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
